package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleDiseasesType;
import com.pubinfo.passbook.common.mapper.TRuleDiseasesTypeMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleDiseasesTypeParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.rule.DiseasesTypeRelevanceVo;
import com.pubinfo.passbook.common.service.master.TRuleDiseasesTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
@Slf4j
public class TRuleDiseasesTypeServiceImpl extends ServiceImpl<TRuleDiseasesTypeMapper, TRuleDiseasesType>
    implements TRuleDiseasesTypeService {
  
  @Override
  public IPage<TRuleDiseasesType> selectDiseasesType(RuleDiseasesTypeParam param) {
    IPage<TRuleDiseasesType> iPage = page(param.getPage("create_time"));
    return this.baseMapper.selectDiseasesType(iPage, param);
  }
  
  @Override
  public JsonResult<String> deleteDiseasesType(String diseasesTypeId) {
    List<DiseasesTypeRelevanceVo> diseasesTypeRelevanceVos = this.baseMapper.selectDiseasesTypeRelevance(diseasesTypeId);
    diseasesTypeRelevanceVos.removeIf(Objects::isNull);
    if (!diseasesTypeRelevanceVos.isEmpty())
      return JsonResult.error("改数据被其他表引用,删除失败");
    this.baseMapper.deleteDistrictDiseases(diseasesTypeId);
    this.baseMapper.deleteById(diseasesTypeId);
    return JsonResult.success("删除成功");
  }

  @Override
  public JsonResult<String> saveDiseasesType(TRuleDiseasesType type) {
    QueryWrapper<TRuleDiseasesType> wrapper = new QueryWrapper();
    wrapper.eq("name",type.getName());
    if (this.baseMapper.selectCount(wrapper) > 0) {
       return JsonResult.error("病种类型名重复");
    }
    this.baseMapper.insert(type);
    return JsonResult.success("添加成功");
  }
}




